package xyz.jpcr.covid19app.Util;

import android.content.Context;
import android.util.Log;

import com.baidu.location.BDAbstractLocationListener;
import com.baidu.location.BDLocation;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;

import xyz.jpcr.covid19app.MainActivity;
import xyz.jpcr.covid19app.R;

public class ILocationUtil {
    private static ILocationUtil iLocationUtil;
    public String province, city, country;
    public LocationClient locationClient = null;
    private ILocationUtil.ILocationListener myListener;
    private Context mcontext;
    private BDLocation mBDLocation;

    public void setProvince(String province) {
        this.province = province;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getProvince() {
        return province;
    }

    public String getCity() {
        return city;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public static ILocationUtil getInstance(Context context) {
        if (iLocationUtil == null) {
            iLocationUtil = new ILocationUtil(context);
        }
        return iLocationUtil;
    }

    public ILocationUtil(Context context) {
        initLocation(context);
        mcontext = context;
    }

    public BDLocation getmBDLocation() {
        return mBDLocation;
    }

    private void initLocation(Context context) {
        myListener = new ILocationListener();
        locationClient = new LocationClient(context);
        locationClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setIsNeedAddress(true);
        option.setOpenGps(true);
//        option.setScanSpan(3000);
        option.setLocationNotify(true);
        locationClient.setLocOption(option);
//        locationClient.start();
    }

    public void startClient() {
        locationClient.start();
    }

    public void stopClient() {
        locationClient.stop();
    }


    /*
     * 定位接口
     * */
    public class ILocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation bdLocation) {
            if (bdLocation.getDistrict() != null) {
                mBDLocation = bdLocation;
//                province = bdLocation.getProvince();
//                toolbar.setTitle(mcontext.getString(R.string.covid_19_tracking) + "(" + bdLocation.getCity() + ")");
//                Log.i(mcontext.toString(), "onReceiveLocation: " + province);
                locationClient.stop();
            }
        }
    }
}
